package com.slz3.business.module.orderManagement.ordRefund.steward;

import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.afterSaleManagement.aftApplyOrder.domain.AftApplyOrder;
import com.slz3.business.module.afterSaleManagement.aftApplyOrder.domain.AftApplyOrderFactory;
import com.slz3.business.module.afterSaleManagement.aftApplyOrder.domain.AfterSale;
import com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.domain.AftApplyOrderLine;
import com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.domain.AftApplyOrderLineFactory;
import com.slz3.business.module.afterSaleManagement.aftApplyOrderLine.domain.AfterSaleDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @FileName: OrdRefundSynr
 * @Description:售后消息同步管理
 */

@Slf4j
@Component
public class OrdRefundSynr {

    /**
     * 退款申请同步处理方法
     */
    public static void ordRefundAdd(){

        String afterSaleUuid;
        try {
            afterSaleUuid = Optional.ofNullable(Jedis.db0.leftPop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_APPLYAFTERSALELIST)).toString()).orElse("");
            //消费逻辑
            log.info("消费消息:" +afterSaleUuid);
            //处理完删除set集合
            if (VE.isNotEmpty(afterSaleUuid)) {
                //根据map先获取订单信息并保存订单
                saveAfterSaleUtilMethod(afterSaleUuid);
                //处理完删除set集合
                RedisClient.redisTemplate.opsForSet().pop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_APPLYAFTERSALELIST.concat("Set")));

            }

        }catch (Exception e){
            //主数据set 集合
            Set members = Jedis.db0.getSet(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_APPLYAFTERSALELIST.concat("Set")));
            Iterator iterator = members.iterator();
            //消费逻辑
            log.info("消费消息:" + members.toString());
            //根据map先获取订单信息并保存订单
            //遍历set集合
            while (iterator.hasNext()) {
                afterSaleUuid = Optional.ofNullable(iterator.next().toString()).orElse("");
                if (VE.isNotEmpty(afterSaleUuid)) {
                    //根据map先获取订单信息并保存订单
                    saveAfterSaleUtilMethod(afterSaleUuid);
                }
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_APPLYAFTERSALELIST.concat("Set")));
        }
    }

    public static void cancelRefund(){
        String afterSaleUuid;
        try {
            afterSaleUuid = Optional.ofNullable(Jedis.db0.leftPop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_RECALLREFUNDLIST)).toString()).orElse("");
            //消费逻辑
            log.info("消费消息:" +afterSaleUuid);
            //处理完删除set集合
            if (VE.isNotEmpty(afterSaleUuid)) {
                //根据map先获取订单信息并保存订单
                saveAfterSaleUtilMethod(afterSaleUuid);
                //处理完删除set集合
                RedisClient.redisTemplate.opsForSet().pop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_RECALLREFUNDLIST.concat("Set")));

            }

        }catch (Exception e){
            //主数据set 集合
            Set members = Jedis.db0.getSet(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_RECALLREFUNDLIST.concat("Set")));
            Iterator iterator = members.iterator();
            //消费逻辑
            log.info("消费消息:" + members.toString());
            //根据map先获取订单信息并保存订单
            //遍历set集合
            while (iterator.hasNext()) {
                afterSaleUuid = Optional.ofNullable(iterator.next().toString()).orElse("");
                if (VE.isNotEmpty(afterSaleUuid)) {
                    //根据map先获取订单信息并保存订单
                    saveAfterSaleUtilMethod(afterSaleUuid);
                }
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_RECALLREFUNDLIST.concat("Set")));
        }
    }

    public static void refundDeliver(){
        String afterSaleUuid;
        try {
            afterSaleUuid = Optional.ofNullable(Jedis.db0.leftPop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_DELIVERLIST)).toString()).orElse("");
            //消费逻辑
            log.info("消费消息:" +afterSaleUuid);
            //处理完删除set集合
            if (VE.isNotEmpty(afterSaleUuid)) {
                //根据map先获取订单信息并保存订单
                saveAfterSaleUtilMethod(afterSaleUuid);
                //处理完删除set集合
                RedisClient.redisTemplate.opsForSet().pop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_DELIVERLIST.concat("Set")));

            }
        }catch (Exception e){
            //主数据set 集合
            Set members = Jedis.db0.getSet(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_DELIVERLIST.concat("Set")));
            Iterator iterator = members.iterator();
            //消费逻辑
            log.info("消费消息:" + members.toString());
            //根据map先获取订单信息并保存订单
            //遍历set集合
            while (iterator.hasNext()) {
                afterSaleUuid = Optional.ofNullable(iterator.next().toString()).orElse("");
                if (VE.isNotEmpty(afterSaleUuid)) {
                    //根据map先获取订单信息并保存订单
                    saveAfterSaleUtilMethod(afterSaleUuid);
                }
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(String.format("%s:%s:%s",RK.Sync,RK.ORDER,RK.ORDERRETURN_DELIVERLIST.concat("Set")));
        }
    }


    public static void saveAfterSaleUtilMethod(String afterSaleUuid){
        String afterSaleInfoKey = RK.SERVICE__AFTER_SALE__INFO__AFTER_SALE_UUID__$UUID$.replace("${uuid}",afterSaleUuid);
        String afterSaleJoinAfterSaleDetailKey = RK.SERVICE__AFTER_SALE__JOIN__AFTER_SALE_DETAIL__AFTER_SALE_UUID__$UUID$.replace("${uuid}",afterSaleUuid);

        Map<String,Object> afterSaleMap = Jedis.db0.getMap(afterSaleInfoKey);
        AfterSale afterSale = AfterSale.builder().build();
        Steward.map2Obj(afterSaleMap,afterSale);
        AftApplyOrder aftApplyOrder = AftApplyOrderFactory.newAftApplyOrderByAfterSale(afterSale);


        Services.aftApplyOrder.save(aftApplyOrder);

        List<String> afterSaleDetailUuidList = Jedis.db0.getSet(afterSaleJoinAfterSaleDetailKey).stream().filter(item->item!=null)
                .map(item->item.toString()).collect(Collectors.toList());

        List<Object> afterSaleDetailMapObj = Jedis.db0.getListByKey(
                afterSaleDetailUuidList.stream().map(item->RK.SERVICE__AFTER_SALE_DETAIL__INFO__AFTER_SALE_DETAIL_UUID__$UUID$.replace("${uuid}",item)).collect(Collectors.toList())
        );

        afterSaleDetailMapObj.stream().filter(item->item!=null).forEach(item->{
            AfterSaleDetail afterSaleDetail = AfterSaleDetail.builder().build();
            Map<String,Object> map = (Map)item;
            Steward.map2Obj(map,afterSaleDetail);
            AftApplyOrderLine aftApplyOrderLine = AftApplyOrderLineFactory.newAftApplyOrderLineByAfterSaleDetail(afterSaleDetail);

            aftApplyOrderLine.setSerAfterSaleId(aftApplyOrder.getUuid());
            aftApplyOrderLine.setAfterSaleNo(Optional.ofNullable(aftApplyOrder.getAfterSaleNo()).orElse(""));
            aftApplyOrderLine.setSerAfterSaleType(Optional.ofNullable(aftApplyOrder.getSerAfterSaleType()).orElse(""));
            aftApplyOrderLine.setSerAuditState(Optional.ofNullable(aftApplyOrder.getSerAuditState()).orElse(""));
            Services.aftApplyOrderLine.save(aftApplyOrderLine);
        });
    }
}